package com.kaiwukj.android.ufamily.mvp.ui.page.service.complain;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpFragment;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ComplainResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */
public class ComplainListFragment extends BaseMvpFragment<ComplainPresenter> implements ServiceContract.ComplainView {
    @BindView(R.id.rv_complain_list)
    RecyclerView rvComplainList;
    @BindView(R.id.smart_fl_complain_list)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private ComplainListAdapter complainListAdapter;

    private int page = 1;
    private int pageSize = 12;

    private boolean isLoadingMore = false;

    public static ComplainListFragment newInstance() {
        Bundle args = new Bundle();
        ComplainListFragment fragment = new ComplainListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerServiceComponent.builder()
                .appComponent(appComponent)
                .serviceModule(new ServiceModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_complain_list;
    }

    @Override
    protected void initView() {
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                mPresenter.requestComplainList(++page, pageSize);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.requestComplainList(page = 1, pageSize);
            }
        });
        initRecyclerView();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        emptyView.show(true);
        mPresenter.requestComplainList(page = 1, pageSize);
    }

    private void initRecyclerView() {
        complainListAdapter = new ComplainListAdapter();
        complainListAdapter.setActivity(getActivity());
        rvComplainList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvComplainList.setAdapter(complainListAdapter);
    }

    @Override
    public void onFetchComplainList(List<ComplainResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            complainListAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            complainListAdapter.setList(results);
            refreshLayout.finishRefresh(true);
        }
        if (complainListAdapter.getItemCount() == 0) {
            emptyView.show("暂没有记录", "");
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.hide();
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
    }

    @Override
    public void onGetQiNiuToken(String token) {

    }
}
